---
title: Short-term Memory
description: Session-based conversation history for immediate context retention
---

Short-term memory provides session-based conversation history using LangGraph's Checkpointer system. It automatically preserves conversation context within a session, enabling agents to maintain coherent multi-turn conversations.

## Quick Start

### Basic Configuration

```python
from langcrew import Crew, Agent
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

# Enable short-term memory
memory_config = MemoryConfig(
    provider="sqlite",
    connection_string="sqlite:///memory.db",
    short_term=ShortTermMemoryConfig(enabled=True)
)

crew = Crew(agents=[agent], memory=memory_config)
```

### Configuration Parameters

| Parameter           | Type        | Description                                    | Default  |
| ------------------- | ----------- | ---------------------------------------------- | -------- |
| `enabled`           | bool        | Enable short-term memory                       | True     |
| `provider`          | str \| None | Storage provider override (inherits if None)   | None     |
| `connection_string` | str \| None | Connection string override (inherits if None)  | None     |

**Advanced Configuration:**

```python
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

# Override storage provider for short-term memory only
memory_config = MemoryConfig(
    provider="sqlite",  # Global provider
    connection_string="sqlite:///global.db",
    short_term=ShortTermMemoryConfig(
        enabled=True,
        provider="postgres",  # Override for short-term only
        connection_string="postgresql://localhost/short_term"
    )
)
```

## How It Works

Short-term memory is managed at the Crew level and automatically propagated to all agents through LangGraph's checkpointer system.

### Crew-Level Management

When you configure memory on a Crew, the checkpointer is created and shared across all agents:

```python
crew = Crew(
    agents=[agent1, agent2],
    memory=memory_config  # Checkpointer managed by Crew
)
```

- Crew creates the checkpointer instance
- LangGraph compiles the graph with the checkpointer
- All agents automatically get conversation history
- No need for agents to explicitly manage checkpoints

### Thread-based Sessions

Short-term memory uses thread IDs to separate different conversation sessions:

```python
from langcrew import Crew

crew = Crew(agents=[agent], memory=memory_config)

# User A's conversation
result_a = crew.kickoff(
    inputs={"user_input": "My name is Alice"},
    thread_id="user_alice"
)

# User B's conversation (separate memory)
result_b = crew.kickoff(
    inputs={"user_input": "My name is Bob"}, 
    thread_id="user_bob"
)

# Alice's follow-up (remembers her name)
result_a2 = crew.kickoff(
    inputs={"user_input": "What's my name?"},
    thread_id="user_alice"  # Same thread ID
)
```

### Automatic Context Injection

Short-term memory automatically injects relevant conversation history:

```python
# First message
crew.kickoff(
    inputs={"user_input": "I like Italian food"},
    thread_id="conversation_1"
)

# Second message - agent remembers the first
crew.kickoff(
    inputs={"user_input": "What kind of food do I like?"},
    thread_id="conversation_1"  # Same thread = remembers context
)
# Response: "You mentioned you like Italian food"
```

## Use Cases

### Customer Support

Maintain conversation context across a support session:

```python
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

# Customer support configuration
memory_config = MemoryConfig(
    provider="postgresql",
    connection_string="postgresql://user:pass@localhost:5432/support_db",
    short_term=ShortTermMemoryConfig(enabled=True)
)

support_agent = Agent(
    role="Customer Support Agent",
    goal="Provide helpful support using conversation context",
    backstory="You remember conversation history to provide contextual help"
)

crew = Crew(agents=[support_agent], memory=memory_config)

# Support session
crew.kickoff(
    inputs={"user_input": "My order #12345 hasn't arrived"},
    thread_id="support_session_789"
)

crew.kickoff(
    inputs={"user_input": "When was it supposed to arrive?"},
    thread_id="support_session_789"  # Remembers order #12345
)
```

### Educational Tutoring

Track learning progress within a tutoring session:

```python
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

# Educational session configuration
memory_config = MemoryConfig(
    provider="sqlite",
    connection_string="sqlite:///education.db",
    short_term=ShortTermMemoryConfig(enabled=True)
)

tutor_agent = Agent(
    role="Math Tutor",
    goal="Teach math concepts building on previous explanations",
    backstory="You remember concepts covered and student's understanding level"
)

crew = Crew(agents=[tutor_agent], memory=memory_config)

# Tutoring session
student_id = "student_123"
crew.kickoff(
    inputs={"user_input": "Explain linear equations"},
    thread_id=f"student_{student_id}"
)

crew.kickoff(
    inputs={"user_input": "Now show me how to solve x + 5 = 10"},
    thread_id=f"student_{student_id}"
)
```

## Storage Providers

Short-term memory supports all LangCrew storage providers:

### In-Memory (Development)

```python
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

memory_config = MemoryConfig(
    provider="memory",  # Fast, no persistence
    short_term=ShortTermMemoryConfig(enabled=True)
)
```

### SQLite (Single-user/Development)

```python
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

memory_config = MemoryConfig(
    provider="sqlite",
    connection_string="sqlite:///memory.db",
    short_term=ShortTermMemoryConfig(enabled=True)
)
```

### PostgreSQL (Production)

```python
from langcrew.memory import MemoryConfig, ShortTermMemoryConfig

memory_config = MemoryConfig(
    provider="postgresql",
    connection_string="postgresql://user:pass@localhost:5432/memory_db",
    short_term=ShortTermMemoryConfig(enabled=True)
)
```

## Troubleshooting

### Memory Not Persisting

- Ensure you're using the same `thread_id` across conversations
- Verify database connection and permissions
- Check that checkpointer is properly configured

### Context Not Injected

- Confirm short-term config has `enabled: True`
- Verify agents are using the same crew instance

### Performance Issues

- Use connection pooling for database providers
- Consider SQLite for single-user applications

## Next Steps

- **[Long-term Memory](/guides/memory/long-term)** - Persistent knowledge storage
- **[Storage Configuration](/guides/memory/storage)** - Configure storage backends
- **[Memory Concepts](/concepts/memory)** - Understand memory architecture
